<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Base class for controller
 */
class MY_Controller extends CI_Controller
{
    /**
     * Constructor
     */

    function __construct()
    {
        parent::__construct();
		trackUser();
        $this->layout='front_inner';
    }

   	

    /**
     * Returns logged admin_id if require session/log in
     * @param type $string
     * @return boolean
     */
	function require_login(){
		$CI = &get_instance();
		if($CI->session->userdata('sess_user_id')){
			return $CI->session->userdata('sess_user_id');
			} else {
			redirect(base_url().'login');
		}
	}

	function setGroupConcateMaxLength()
	{
		$sql = "SET SESSION group_concat_max_len = 1000000";
		$this->db2->query($sql);
	}
	
	function checkValidYMD($date)
    {
        if (preg_match("/^(\d{4}-\d{1,2}-\d{1,2})$/", $date))
        {
            list($year, $month, $day) = explode("-", $date);
            if (checkdate($month, $day, $year))
            {
                return TRUE;
            }
        }
        $this->form_validation->set_message('checkValidYMD', 'The %s field must contain valid date.');
        return FALSE;
    }
	
	function checkValidTime($time)
    {
        if (preg_match("/^(\d{2}:\d{2})$/", $time))
        {
            list($hours, $min) = explode(":", $time);
			$hours = (int) $hours;
			$min = 	(int) $min;
            if ( $hours >= 0 && $hours <= 23 && $min >= 0 && $min <= 59)
            {
                return TRUE;
            }
        }
        $this->form_validation->set_message('checkValidTime', 'The %s field must contain valid time.');
        return FALSE;
    }
}
